package com.brin.code.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileUploadException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.brin.code.model.pojo.Sample;
import com.brin.code.model.vo.SampleVO;
import com.brin.code.service.SampleEntityService;
import com.brin.code.service.SampleService;
import com.brin.core.util.StringUtil;

@Controller
public class DemoController {

	@RequestMapping("/demo/demo0")
	public ModelAndView demo0(Sample vo,Map<String, Object> model) {
		vo.setSampleName("您好！");
		vo.setTotalRows(sampleService.queryCountSample());
		List<SampleVO> list = sampleService.querySampleList(vo);
		model.put("vo", vo);
		model.put("samples", list);
		return new ModelAndView("/demo/demo");
	}
	
	@RequestMapping("/demo/demo1")
	public @ResponseBody String addSample(Sample sample){
		System.out.println(sample.getSampleName());
		if(sampleEntityService.addSample(sample)){
			return "1";
		}
		return "0";
	}
	
	@RequestMapping("/demo/demo2")
	public @ResponseBody String testJson(Sample sample){
		System.out.println(sample.getSampleName());
		return "1";
	}
	
	@RequestMapping("/demo/edit")
	public ModelAndView querySample(Sample sample, Map<String, Object> model){
		System.out.println("id:" + sample.getId());
		SampleVO vo = sampleService.querySampleById(sample.getId());
		model.put("sample", vo);
		return new ModelAndView("/demo/detail");
	}
	
	@RequestMapping("/demo/memcachedTest")
	public @ResponseBody String memcacheTest(Sample sample){
		SampleVO vo = sampleService.querySampleById(sample.getId());
		if(vo != null){
			System.out.println(vo.getSampleName());
			return "1";
		}
		return "0";
	}
	
//	@RequestMapping("/demo/demo1")
//	public @ResponseBody String sample20(Map<String, Object> model) {
//		
//		return MemUtil.get("area").toString();
//	}
	
//	@SuppressWarnings("finally")
//	@RequestMapping("/demo/demo2")
//	public String sample2(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) {
//		try {
//			String sampleName = ServletRequestUtils.getStringParameter(request, "sampleName");
//			model.put("sampleName", StringUtil.isBlank(sampleName)?"sampleName2为空":sampleName + " 通过get请求");
//		} catch (Exception e) {
//			LOG.error(e.getMessage(), e);
//		} finally {
//			return "/sample/sample2";
//		}
//	}
	
	@RequestMapping("/demo/demo3")
	public void sample3(HttpServletRequest request, Map<String, Object> model) {
		try {
			String sampleName = ServletRequestUtils.getStringParameter(request, "sampleName");
			model.put("sampleName", StringUtil.isBlank(sampleName)?"sampleName3为空":sampleName + " 通过get请求");
		} catch (Exception e) {
//			LOG.error(e.getMessage(), e);
		}
	}
	
	@RequestMapping("/demo/demo4")
	public void sample4(SampleVO vo, Map<String, Object> model) {
		vo.setSampleName("您好！");
		model.put("vo", vo);
	}
	
	@RequestMapping("/demo/demo5")
	public void sample5(@RequestParam("sampleName") String sampleName, Map<String, Object> model) {
		model.put("sampleName", sampleName);
	}
	
	@RequestMapping("/demo/demo6")
	public @ResponseBody SampleVO sample6(SampleVO vo, Map<String, Object> model) {
		vo.setSampleName("yes");
		
		return vo;
	}
	
	/**
	 * 如用此方法，可不用继承BaseController
	 * @param vo
	 * @param model
	 * @return
	 */
	@RequestMapping("/demo/demo7")
	public @ResponseBody SampleVO sample7(SampleVO vo, Map<String, Object> model) {
		vo.setSampleName("小冀冀觉悟了！");
		
		return vo;
	}
	
	@RequestMapping("/demo/demo8")
	public @ResponseBody SampleVO sample8(SampleVO vo, Map<String, Object> model) {
//		sampleEntityService.create2();
		vo.setId(1234);
		
		return vo;
	}
	
	/**
	 * 上传
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/demo/demo9")
	public void  sample9(Map<String, Object> model) {
		
	}
	
	@RequestMapping("/demo/demo10")
	public String  sample10(@RequestParam(value = "file", required = false) MultipartFile file, HttpServletRequest request) throws FileUploadException, IOException {
		String fileName = file.getOriginalFilename();
		if (StringUtil.isBlank(fileName)) {
//			LOG.error("上传文件为空");
			
			return "/demo/demo9";
		}
		
		if (fileName.indexOf(".xls") == -1) {
//			LOG.error("上传文件不是Excel文件");
			return "/demo/demo9";
		}
		
		FileCopyUtils.copy(file.getBytes(), new File("F:/upload/_tmp_" + new Date().getTime() + ".xls"));
		
		return "/demo/demo9";
	}
	
	@RequestMapping("/demo/demo11")
	public String sample11(HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) {
		
		return "/demo/demo11";
	}
	
	@RequestMapping("/demo/demo12")
	public String sample12(SampleVO sample, HttpServletRequest request, HttpServletResponse response, Map<String, Object> model) {
		List<String> list1 = new ArrayList<String>();
		for (int i = 32; i < 64; i++) {
			list1.add(String.valueOf((char) i));
		}
		
		List<String> list2 = new ArrayList<String>();
		for (int i = 97; i < 128; i++) {
			list2.add(String.valueOf((char) i));
		}
		
		Random random = new Random();
		
		model.put("list1", list1);
		model.put("list2", list2);
		model.put("random", random.nextInt(10));
		
		return "/demo/demo12";
	}
	
	@Autowired
	private SampleService sampleService;
	
	@Autowired
	private SampleEntityService sampleEntityService;

	public SampleEntityService getSampleEntityService() {
		return sampleEntityService;
	}
	public void setSampleEntityService(SampleEntityService sampleEntityService) {
		this.sampleEntityService = sampleEntityService;
	}

	public SampleService getSampleService() {
		return sampleService;
	}
	public void setSampleService(SampleService sampleService) {
		this.sampleService = sampleService;
	}
}
